package com.guanmi.ethereum.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.http.HttpService;

import com.guanmi.ethereum.configure.EthConfigure;

/**
 */
@Component
public class EthereumTransactionListener implements ApplicationListener<ContextRefreshedEvent> {

	private static final Logger log = LoggerFactory.getLogger(EthereumTransactionListener.class);

	@Autowired
	private EthConfigure ethConfigure;

	public void onApplicationEvent(ContextRefreshedEvent evt) {

		if (evt.getApplicationContext().getParent() != null) {
			return;
		}

		// 监听用户转入的交易记录
		Web3j web3j = Web3j.build(new HttpService(ethConfigure.getNodeUrl()));
		web3j.transactionFlowable().subscribe(tx -> {

			//根据交易HASH可以查询 转入转出信息
			log.info("transaction code {}", tx.getHash());
		});

	}
}
